package com.mgq.netty.chat.session;

import io.netty.channel.Channel;

import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author MaGuangQi
 * @description
 * @date 2021-12-08 09:55
 **/
public class SessionImpl implements Session {
    private ConcurrentHashMap<Channel, String> channelUsername = new ConcurrentHashMap<>();
    private ConcurrentHashMap<String, Channel> usernameChannel = new ConcurrentHashMap<>();

    @Override
    public void bind(Channel channel, String username) {
        channelUsername.put(channel,username);
        usernameChannel.put(username,channel);
    }

    @Override
    public void unbind(Channel channel) {
        String username = channelUsername.remove(channel);
        if (Objects.nonNull(username)) {
            usernameChannel.remove(username);
        }

    }

    @Override
    public Channel getChannel(String username) {
        return usernameChannel.get(username);
    }
}
